package com.hhxy.game;


import org.junit.Test;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class GameDemo {
    /**
     * 1.定义一个动态的集合
     *  用来存储54张牌
     */
    public static List<Card> allCards = new ArrayList<>();
    /**
     * 2.使用静态代码快
     *  用来生成一副牌
     */
    static{
        /**
         *  3.定义点数
         *  大王小王没有点数和花色需要单独定义
         */
        String[] sizes = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] colors = {"♠","♥","♣","♦"};
        //4.定义普通牌]\
        int index=0;//用于定义牌的大小
        for (int i = 0; i <sizes.length ; i++) {
            index++;
            for (int j = 0; j <colors.length ; j++) {
                Card card = new Card(sizes[i],colors[j],index);
                allCards.add(card);
            }
        }
        //5.定义大小王
        Card c1 = new Card("小","🃏",++index);
        Card c2 = new Card("大","🃏",++index);
        Collections.addAll(allCards,c1,c2);
        //6.输出做好的这副牌
        System.out.println(allCards.size()+"张新牌："+allCards);
    }

    public static void main(String[] args) {
        //7.洗牌，随机打乱牌的顺序
        Collections.shuffle(allCards);
        System.out.println("洗好后的牌为："+allCards);
        //8.定义三个玩家
        List<Card> linhuchong = new ArrayList<>();
        List<Card> jiumozhi = new ArrayList<>();
        List<Card> renyingying = new ArrayList<>();
        //9.从集合种发出51张牌分别给三个玩家，留三张地主牌
        for (int i = 0; i < allCards.size()-3; i++) {
            Card c = allCards.get(i);
            //轮流发给三个人
            if(i%3==0){//发给linhuchong
                linhuchong.add(c);
            }else if(i%3==1){
                jiumozhi.add(c);
            }else{
                renyingying.add(c);
            }
        }
        //10.把最后三张牌放到一个集合中
        List<Card> lastThreeCards = allCards.subList(allCards.size()-3,allCards.size());
        //11.给玩家的牌进行排序
        sortCards(linhuchong);
        sortCards(jiumozhi);
        sortCards(renyingying);
        //12.输出玩家的牌
        System.out.println("令狐冲："+linhuchong);
        System.out.println("鸠摩智："+jiumozhi);
        System.out.println("任盈盈："+renyingying);
        System.out.println("地主牌"+lastThreeCards);
    }

    private static void sortCards(List<Card> cards) {
        //排序规则：大王>小王>2>A>K>Q>J>10……
/*        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.getIndex()-o1.getIndex();
            }
        });*/
        Collections.sort(cards,(o1,o2)->o2.getIndex()-o1.getIndex());//降序排列
    }
}

